package com.wgts.wgts.mvp.view.activity.tasklobby;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.eventbus.TaskDetailsEventBus;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMyJoinTaskActivityCommponent;
import com.wgts.wgts.dagger.module.MyJoinTaskActivityModule;
import com.wgts.wgts.mvp.presenter.IMyJoinTaskActivityPresenter;
import com.wgts.wgts.mvp.view.IMyJoinTaskActivityView;
import com.wgts.wgts.netmodle.resp.MyJoinTaskEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 我参与的
 *
 */public class MyJoinTaskTaskActivity extends BaseActivity implements IMyJoinTaskActivityView {
    @Inject
    IMyJoinTaskActivityPresenter presenter;
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.my_join_task_recycle)
    RecyclerView myJoinTaskRecycle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private MyJoinTaskAdapter adapter;

    private List<MyJoinTaskEntity> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wgts_my_join_task_activity);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        includeTitle.setText("任务记录");
        initView();
    }

    private void initView() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);
        myJoinTaskRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyJoinTaskAdapter(this, list);
        myJoinTaskRecycle.setAdapter(adapter);
        adapter.setOnClickListern(new MyJoinTaskAdapter.onClickListern() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(MyJoinTaskTaskActivity.this, WgtsTaskDetailsActivity.class);
                intent.putExtra(WgtsTaskDetailsActivity.TASK_ID, list.get(position).userTaskId);
                intent.putExtra(WgtsTaskDetailsActivity.COME_TYPE, WgtsTaskDetailsActivity.JOIN_AND_COMMIT);
                startActivity(intent);
            }
        });
        load();
        presenter.MyJoinTask();
    }

    @Override
    public void init() {
        DaggerMyJoinTaskActivityCommponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .myJoinTaskActivityModule(new MyJoinTaskActivityModule(this))
                .build()
                .inject(this);
        list = new ArrayList<>();
    }

    @Override
    public void onError(int status, String msg) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        noneDataLy.setVisibility(View.GONE);
        netErrorLy.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.back_btn_ly)
    public void onViewClicked() {
        finish();
    }

    private void load() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore();
            }
        });
    }

    @Override
    public void myJoinTask(List<MyJoinTaskEntity> entity, boolean noMore) {
        list.clear();
        list.addAll(entity);
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        if (entity.size() > 0){
            noneDataLy.setVisibility(View.GONE);
            netErrorLy.setVisibility(View.GONE);
        }else {
            noneDataLy.setVisibility(View.VISIBLE);
            netErrorLy.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadMore(List<MyJoinTaskEntity> entities, boolean noMore) {
        refreshLayout.finishLoadmore();
        list.addAll(entities);
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TaskDetailsEventBus event) {
        presenter.MyJoinTask();
    }
}
